<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :form_data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow;
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = item.type == 'input_number' ? ['', ''] : '';
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="c_666 fw">消费明细</div>
        <div class="flex">
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable;
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true;
                });
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e;
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :max-height="max_height"
        :data="list"
        border
        highlight-current-row
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height: height_size == 1 ? '58px' : height_size == 2 ? '48px' : '38px',
        }"
      >
        <el-table-column type="index" label="序号" width="80px" fixed></el-table-column>
        <el-table-column
          label="消费订单号"
          key="消费订单号"
          prop="orderId"
          v-if="header_list[0].showTable"
          sortable
          width="200"
        ></el-table-column>
        <el-table-column
          label="消费日期"
          key="消费日期"
          sortable
          prop="createTime"
          width="180"
          v-if="header_list[1].showTable"
          ><template #default="scope: any"
            ><div v-if="scope.row.createTime != '-1'">
              {{ formatDate(parseInt(scope.row.createTime), "yyyy-MM-dd hh:mm:ss") }}
            </div>
          </template></el-table-column
        >
        <el-table-column
          label="消费金额"
          key="消费金额"
          sortable
          v-if="header_list[2].showTable"
          prop="allPayTotalFee"
        ></el-table-column>
        <el-table-column
          label="消费项目"
          key="消费项目"
          sortable
          v-if="header_list[3].showTable"
          width="200"
          ><template #default="scope: any">
            <div v-for="item in productItemList" :key="item.id">
              <span
                v-if="
                  item.productId == scope.row.productId && item.code == scope.row.code
                "
                >{{ item.name }}</span
              >
            </div>
          </template></el-table-column
        >
        <el-table-column
          label="产品类别"
          key="产品类别"
          sortable
          v-if="header_list[4].showTable"
          prop=""
        >
          <template #default="scope: any">
            <div v-for="item in store.state.product_list" :key="item">
              <span v-if="scope.row.productId == item.dictCode">{{
                item.dictValue
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="抵用券号"
          key="抵用券号"
          sortable
          v-if="header_list[5].showTable"
          prop=""
        ></el-table-column>
        <el-table-column
          label="抵用金额"
          key="抵用金额"
          sortable
          v-if="header_list[6].showTable"
          prop=""
        ></el-table-column>
        <el-table-column
          label="消费账号"
          key="消费账号"
          sortable
          prop="operatorName"
          v-if="header_list[7].showTable"
        ></el-table-column>
        <el-table-column
          label="项目名称"
          key="项目名称"
          sortable
          prop="projectName"
          v-if="header_list[8].showTable"
        ></el-table-column
        ><el-table-column
          label="工程名称"
          key="工程名称"
          sortable
          prop="engineeringName"
          v-if="header_list[9].showTable"
        ></el-table-column>
        <el-table-column
          label="开票状态"
          key="开票状态"
          sortable
          prop=""
          v-if="header_list[10].showTable"
        ></el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        style="bottom: 177px"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
      <div class="bottom_box">
        <div style="background-color: #f0f2f5; height: 10px"></div>
        <p>余额信息</p>
        <div>
          <div
            class="flex"
            :style="{
              backgroundImage: 'url(' + require('@/assets/img/re_detail_1.png') + ')',
            }"
          >
            <div>
              <p>充值余额</p>
              <p>{{ balance_info?.amount }}</p>
            </div>
          </div>
          <div
            class="flex"
            :style="{
              backgroundImage: 'url(' + require('@/assets/img/re_detail_3.png') + ')',
            }"
          >
            <div>
              <p>消费总额</p>
              <p>{{ balance_info?.totalCostAmount }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { check_role, formatDate, handle_search_key, debounce } from "@/utils/check_role"; // 权限校验
import search from "@/view/components/search.vue";
import setTable from "@/view/components/setTable.vue";
import axios from "@/service/HttpRequest";
import serviceApi from "@/service/api";
import { ref, computed, onMounted } from "vue";
import { useStore } from "vuex";
import EventBus from "@/utils/EventBus";
import { ElMessage } from "element-plus";
const store = useStore();
const header_list = ref<any>([
  {
    label: "消费订单号",
    value: "",
    showTable: true,
    type: "input",
    isShow: true,
    options: [],
  },
  {
    label: "消费日期",
    value: "",
    showTable: true,
    type: "date",
    isShow: true,
    options: [],
  },
  {
    label: "消费金额",
    value: ["", ""],
    showTable: true,
    type: "input_number",
    isShow: false,
    options: [],
  },
  {
    label: "消费项目",
    value: "",
    showTable: true,
    type: "select",
    isShow: false,
    options: computed(() => {
      return productItemList.value;
    }),
  },
  {
    label: "产品类别",
    value: "",
    showTable: true,
    type: "select",
    isShow: false,
    options: computed(() => {
      return store.state.product_list;
    }),
  },
  {
    label: "抵用券号",
    value: "",
    showTable: true,
    type: "input",
    isShow: false,
    options: [],
  },
  {
    label: "抵用金额",
    value: ["", ""],
    showTable: true,
    type: "input_number",
    isShow: false,
    options: [],
  },
  {
    label: "消费账号",
    value: "",
    showTable: true,
    type: "input",
    isShow: false,
    options: [],
  },
  {
    label: "项目名称",
    value: "",
    showTable: true,
    type: "input",
    isShow: false,
    options: [],
  },
  {
    label: "工程名称",
    value: "",
    showTable: true,
    type: "input",
    isShow: false,
    options: [],
  },
  {
    label: "开票状态",
    value: "",
    showTable: true,
    type: "input",
    isShow: false,
    options: [],
  },
]);
const max_height = ref();
const height_size = ref(1);
const page_data = ref({
  current: 1,
  pageSize: 10,
  allPayTotalFee: "",
  engineeringName: "",
  projectName: "",
  operatorName: "",
  orderId: "",
  code: "",
  createTime: "",
  productId: "",
});
const balance_info = ref(
  computed(() => {
    return store.state.balance_info;
  })
);
onMounted(() => {
  max_height.value = window.innerHeight - 490; // 不带分页的 table 高度
  EventBus.on("refresh", () => {
    get_list();
    ElMessage.success("刷新成功！");
  });
  get_list();
  get_productItemList();
});
const productItemList = ref();
const get_productItemList = () => {
  axios
    .request({ url: serviceApi.productItemList, method: "post" })
    .then((res) => {
      console.log(res);
      res.data.forEach((item) => {
        item.label = item.name;
        item.value = item.code + "-" + item.productId;
      });
      productItemList.value = res.data;
    })
    .catch((err) => {
      ElMessage.error(err);
    });
};
const list = ref([]);
const total = ref<number>();
const loading = ref();
const get_list = () => {
  let data = JSON.parse(JSON.stringify(handle_search_key(page_data.value)));
  data.code.type = "EQUAL";
  axios
    .request({
      url: serviceApi.payOrder_list,
      method: "post",
      data: data,
    })
    .then((res) => {
      console.log(res);
      list.value = res.data.content;
      total.value = parseInt(res.data.totalElements);
    })
    .catch((err) => {
      ElMessage.error(err);
    });
};

let list_debounce = debounce(200, get_list);
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    orderId: header_list.value[0].value,
    createTime: header_list.value[1].value,
    allPayTotalFee: header_list.value[2].value,
    code: header_list.value[3].value.split("-")[0],
    productId:
      header_list.value[4].value == ""
        ? header_list.value[3].value.split("-")[1]
        : header_list.value[4].value,
    operatorName: header_list.value[7].value,
    projectName: header_list.value[8].value,
    engineeringName: header_list.value[9].value,
  };
  get_list();
};
</script>

<style lang="scss" scoped>
.bottom_box {
  position: absolute;
  bottom: 20px;
  left: 0;
  width: 100%;
  > p {
    font-size: 16px;
    font-weight: bold;
    margin: 15px 20px;
  }
  > div {
    display: flex;
    align-items: center;
    color: #fff;
    > div {
      display: flex;
      align-items: center;
      background-size: 100% 100%;
      width: 190px;
      height: 90px;
      margin: 0 0 0 20px;
      > div {
        margin-left: 16px;
      }
    }
    > div p:first-child {
      font-size: 14px;
    }
    > div p:last-child {
      font-size: 24px;
      font-weight: bold;
    }
  }
}
</style>
